package com.ruoyi.pcwl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pcwl.domain.*;
import com.ruoyi.pcwl.domain.enums.CarouselTypeEnum;
import com.ruoyi.pcwl.domain.vo.CarouselVo;
import com.ruoyi.pcwl.service.AppVedioService;
import com.ruoyi.pcwl.service.CarouselService;
import com.ruoyi.pcwl.service.IAppCarouselService;
import com.ruoyi.pcwl.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章信息，存储已发布的文章Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Api(tags = "小程序端内容获取接口")
@RestController
@RequestMapping("/app/article")
public class AppController extends BaseController
{
    @Autowired
    private IArticleService articleService;

//    @Autowired
//    IAppCarouselService appCarouselService;

    @Resource
    private CarouselService carouselService;

    @Autowired
    private AppVedioService appVedioService;

    /**
     * 查询文章信息，存储已发布的文章列表
     */
    @ApiOperation("分页获取已发布到小程序的视频列表")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/listAppVedio")
    public TableDataInfo listAppVedio(AppVedio appVedio)
    {
        Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum")==null?1:ServletUtils.getParameter("pageNum"));
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize")==null?10:ServletUtils.getParameter("pageSize"));
        Page<AppVedio> appVedioPage = new Page<>(pageNum, pageSize);
        IPage<AppVedio> appVedioIPage = appVedioService.getPublishVedio(appVedioPage,appVedio);
        return getDataTable(appVedioIPage);
    }


    /**
     * 查询文章信息，存储已发布的文章列表
     */
    @ApiOperation("分页获取已发布到小程序的文章列表")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/listAppArticle")
    public TableDataInfo listArticle(Article article)
    {
        Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum")==null?1:ServletUtils.getParameter("pageNum"));
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize")==null?10:ServletUtils.getParameter("pageSize"));
        Page<Article> articlePage = new Page<>(pageNum, pageSize);
        IPage<Article> articleIPage = articleService.getPublishArticle(articlePage,article);
        return getDataTable(articleIPage);
    }

    /**
     * 查询APP已发布文章内容列表
     */
    @ApiOperation("获取小程序首页的轮播图")
//    @PreAuthorize("@ss.hasPermi('system:content:list')")
    @GetMapping("/getAppCarousel")
    public AjaxResult getAppCarousel()
    {
       CarouselVo appCarouselList= carouselService.getCarousel(CarouselTypeEnum.HOMEPAGE);

        return AjaxResult.success(appCarouselList);
    }

    public TableDataInfo getDataTable(IPage page)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

}
